﻿using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.ViewModels;
using ShopB2C.Helper;
using ShopB2C.Enums;

namespace ShopB2C.Controllers.Manager {
    /// <summary>
    /// 搜索
    /// </summary>
    public partial class ConfigController : BaseController {
        [OutputCache(Duration = 0, VaryByParam = "*")]
        public ActionResult Search([DefaultValue(1)]int number) {
            int pageCount;
            return View(new SearchViewModel() {
                HotKeys = ConfigHelper.GetValue(ConfigNodeNames.Search, "hotkeys"),
                DefaultKey = ConfigHelper.GetValue(ConfigNodeNames.Search, "defaultkey"),
                ProductSearchKeysList = _psk.GetProductSearchKeysList(this.PageSize, number, out pageCount),
                PageNumber = number,
                Pager = this.CreatePager(string.Format("javascript:AjaxPage('{0}');",
                    Url.Action("Search", new { number = "{0}" })), pageCount, number)
            });
        }

        [HttpPost]
        public ActionResult SearchConfigSave(SearchViewModel svm) {
            string msg = "保存配置操作成功完成!";
            try {
                ConfigHelper.SetValue(ConfigNodeNames.Search, "hotkeys", svm.HotKeys);
                ConfigHelper.SetValue(ConfigNodeNames.Search, "defaultkey", svm.DefaultKey);
                ConfigHelper.Save();
            }
            catch {
                msg = "发生错误, 保存配置操作失败!";
            }
            return Content(msg, "text/plain");
        }

        [HttpPost]
        public ActionResult SearchEdit() {
            return View(new SearchViewModel() {
                ProductSearchKeys = new ProductSearchKeysEntity()
            });
        }

        [HttpPost]
        public ContentResult SearchEditSave(SearchViewModel svm) {
            ProductSearchKeysEntity pske = svm.ProductSearchKeys;
            _psk.ProductSearchKeysAppend(pske);

            return Content("成功完成操作!", "text/plain");
        }

        public ActionResult SearchDelete(string id) {
            _psk.ProductSearchKeysDelete(id);

            return Content("成功完成删除操作!", "text/plain");
        }

    }
}
